package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IFormGroupFieldValueDAO;
import org.jsola.hr.dto.FormGroupFieldValueAddDTO;
import org.jsola.hr.dto.FormGroupFieldValueUpdateDTO;
import org.jsola.hr.entity.FormGroupFieldValueDO;
import org.jsola.hr.query.FormGroupFieldValueQuery;
import org.jsola.hr.service.IFormGroupFieldValueService;
import org.jsola.hr.vo.FormGroupFieldValueListVO;
import org.jsola.hr.vo.FormGroupFieldValueVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 自定义字段存值
 *
 * @author zhr
 */
@Service("hrFormGroupFieldValueServiceImpl")
public class FormGroupFieldValueServiceImpl implements IFormGroupFieldValueService {

    @Autowired
    private IFormGroupFieldValueDAO formGroupFieldValueDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public FormGroupFieldValueVO save(FormGroupFieldValueAddDTO formGroupFieldValueAddDTO, TokenUser tokenUser) {
        // 转DO
        FormGroupFieldValueDO formGroupFieldValueDO = formGroupFieldValueAddDTO.to(FormGroupFieldValueDO.class);
        // 保存
        formGroupFieldValueDO = save(formGroupFieldValueDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return formGroupFieldValueDO.to(FormGroupFieldValueVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public List<FormGroupFieldValueVO> batchSave(List<FormGroupFieldValueAddDTO> formGroupFieldValueAddDTOList, TokenUser tokenUser) {
        if (CollectionUtils.isEmpty(formGroupFieldValueAddDTOList)) {
            return null;
        }
        List<FormGroupFieldValueDO> formGroupFieldValueDOList = formGroupFieldValueAddDTOList
                .parallelStream()
                .map(formGroupFieldValueAddDTO -> {
                    FormGroupFieldValueDO formGroupFieldValueDO = formGroupFieldValueAddDTO.to(FormGroupFieldValueDO.class);
                    formGroupFieldValueDO.preInsert(tokenUser.getUserId());
                    formGroupFieldValueDO.setSiteId(tokenUser.getSiteId());
                    return formGroupFieldValueDO;
                }).collect(Collectors.toList());
        formGroupFieldValueDAO.insertListAndSetId(formGroupFieldValueDOList);
        return formGroupFieldValueDOList
                .parallelStream()
                .map(formGroupFieldValueDO -> formGroupFieldValueDO.to(FormGroupFieldValueVO.class))
                .collect(Collectors.toList());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(FormGroupFieldValueUpdateDTO formGroupFieldValueUpdateDTO, TokenUser tokenUser) {
        //转DO
        FormGroupFieldValueDO formGroupFieldValueDO = formGroupFieldValueUpdateDTO.to(FormGroupFieldValueDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(formGroupFieldValueDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, Long... formGroupFieldValueIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) formGroupFieldValueIds);
    }


    @Override
    public FormGroupFieldValueVO selectById(Long formGroupFieldValueId, String siteId) {
        FormGroupFieldValueDO formGroupFieldValueDO = selectDOById(formGroupFieldValueId, siteId);
        if (formGroupFieldValueDO == null) {
            return null;
        }
        return formGroupFieldValueDO.to(FormGroupFieldValueVO.class);
    }

    @Override
    public List<FormGroupFieldValueListVO> select(FormGroupFieldValueQuery formGroupFieldValueQuery, String siteId) {
        List<FormGroupFieldValueDO> formGroupFieldValueDOList = selectDO(formGroupFieldValueQuery, siteId);
        if (CollectionUtils.isEmpty(formGroupFieldValueDOList)) {
            return formGroupFieldValueDOList == null ? null : new ArrayList<>();
        }
        return formGroupFieldValueDOList.stream()
                .map(formGroupFieldValueDO -> formGroupFieldValueDO.to(FormGroupFieldValueListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(FormGroupFieldValueQuery formGroupFieldValueQuery, String siteId) {
        Example example = buildExample(formGroupFieldValueQuery, siteId);
        return formGroupFieldValueDAO.selectCountByExample(example);
    }

    @Override
    public Page<FormGroupFieldValueListVO> selectPage(FormGroupFieldValueQuery formGroupFieldValueQuery, String siteId) {
        Example example = buildExample(formGroupFieldValueQuery, siteId);
        Page<FormGroupFieldValueDO> page = formGroupFieldValueDAO.selectPageByExample(example,
                formGroupFieldValueQuery.getPageNo(),
                formGroupFieldValueQuery.getPageSize());

        return page.to(FormGroupFieldValueListVO.class);
    }

    @Override
    public FormGroupFieldValueDO selectDOById(Long formGroupFieldValueId, String siteId) {
        return listById(formGroupFieldValueId, siteId);
    }

    @Override
    public List<FormGroupFieldValueDO> selectDO(FormGroupFieldValueQuery formGroupFieldValueQuery, String siteId) {
        Example example = buildExample(formGroupFieldValueQuery, siteId);
        return formGroupFieldValueDAO.selectByExample(example);
    }

    @Override
    public List<FormGroupFieldValueDO> selectBySubject(String subjectType, List<String> gysIds) {
        if (CollectionUtils.isEmpty(gysIds)) {
            return new ArrayList<>();
        }
        Example example = new Example(FormGroupFieldValueDO.class);
        example.and().andEqualTo("valid", true)
                .andEqualTo("subjectType", subjectType)
                .andIn("subjectId", gysIds);
        return formGroupFieldValueDAO.selectByExample(example);
    }

    @Override
    public List<FormGroupFieldValueDO> selectBySubjectIdList(List<String> subjectIdList, TokenUser tokenUser) {
        if (CollectionUtils.isEmpty(subjectIdList)) {
            return new ArrayList<>();
        }
        Example example = new Example(FormGroupFieldValueDO.class);
        example.and()
                .andIn("subjectId", subjectIdList)
                .andEqualTo("valid", true)
                .andEqualTo("siteId", tokenUser.getSiteId());
        return formGroupFieldValueDAO.selectByExample(example);
    }

    @Override
    public FormGroupFieldValueDO selectByFieldId(Long fieldId, String subjectId, String subjectType) {
        Example example = new Example(FormGroupFieldValueDO.class);
        example.and().andEqualTo("valid", true)
                .andEqualTo("fieldId", fieldId)
                .andEqualTo("subjectType", subjectType);
        if (!StringUtils.isEmpty(subjectId)) {
            example.and().andEqualTo("subjectId", subjectId);
        }
        List<FormGroupFieldValueDO> list = formGroupFieldValueDAO.selectByExample(example);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public List<FormGroupFieldValueDO> findByFieldId(Long fieldId, String subjectType) {
        Example example = new Example(FormGroupFieldValueDO.class);
        example.and().andEqualTo("valid", true)
                .andEqualTo("fieldId", fieldId)
                .andEqualTo("subjectType", subjectType);
        List<FormGroupFieldValueDO> list = formGroupFieldValueDAO.selectByExample(example);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.stream().filter(field -> !StringUtils.isEmpty(field.getContent())).collect(Collectors.toList());
    }

    /**
     * 根据查询参数，构建example
     *
     * @param formGroupFieldValueQuery 查询参数
     * @param siteId                   所属站点id
     * @return example
     */
    private Example buildExample(FormGroupFieldValueQuery formGroupFieldValueQuery, String siteId) {
        Example example = new Example(FormGroupFieldValueDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (formGroupFieldValueQuery.getFieldId() != null) {
            example.and().andEqualTo("fieldId", formGroupFieldValueQuery.getFieldId());
        }
        if (formGroupFieldValueQuery.getContent() != null) {
            example.and().andEqualTo("content", formGroupFieldValueQuery.getContent());
        }
        if (formGroupFieldValueQuery.getSubjectType() != null) {
            example.and().andEqualTo("subjectType", formGroupFieldValueQuery.getSubjectType());
        }
        if (formGroupFieldValueQuery.getSubjectId() != null) {
            example.and().andEqualTo("subjectId", formGroupFieldValueQuery.getSubjectId());
        }
        // 排序
        ExampleKit.setExampleOrder(example, formGroupFieldValueQuery.getOrders());
        return example;
    }
}




